<script >
import Cookie from 'js-cookie'
import { getMenu } from "@/api";

export default {
  data() {
    return {
      form: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur'}
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    submit() {
      this.$refs.form.validate((val) => {
        if(val) {
          getMenu(this.form).then(res => {
            if(res.data.code === 20000) {
              this.$message({
                message: '登录成功',
                type: 'success'
              });
              Cookie.set('token', res.data.data.token)
              // 获取 menu 数据，存入 store，但是刷新会消失
              this.$store.commit('setMenu', res.data.data.menu)
              // 动态添加路由
              this.$store.commit('addMenu', this.$router)
              this.$router.push('/home')

            }else {
              this.$message.error(res.data.data.message);
            }
          })
        }

      })

    }

  }
}
</script>

<template>
  <el-form ref="form" label-width="70px" class="login-container" :inline="true" :model="form" :rules="rules" >
    <h3 class="login_title">系统登录</h3>
    <el-form-item label="用户名" prop="username">
      <el-input v-model="form.username" placeholder="请输入用户名"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="password">
      <el-input type="password" v-model="form.password" placeholder="请输入密码"></el-input>
    </el-form-item>
    <el-form-item >
      <el-button type="primary" @click="submit">登录</el-button>
    </el-form-item>
  </el-form>



</template>

<style scoped lang="less">
  .login-container {
    width: 350px;
    border: 1px solid #eaeaea;
    margin: 180px auto;
    padding: 35px 35px 15px 35px;
    background-color: white;
    border-radius: 15px;
    box-shadow: 0 0 25px #cac6c6;
    box-sizing: border-box;

    .el-input {
      width: 198px;

    }

    .login_title {
      text-align: center;
      margin-bottom: 40px;
      color: #505458;
    }

    .el-button {
      margin-left: 105px;
      margin-top: 10px;
    }


  }

</style>